package co.fitstart.admin.web.controller.courseware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.GroupTemplateService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.dietary.DietaryProgramTemplate;
import co.fitstart.entity.courseware.dietary.DietaryProgramType;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;
import co.fitstart.entity.courseware.fitnessgroup.SpecialCoursewareTemplate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/fitness-groups/{fitnessGroupId}/special-courseware-templates")
@MenuSetting("menu-course")
public class SpecialCoursewareTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(SpecialCoursewareTemplateController.class);
    
    @Autowired
    private GroupTemplateService groupTemplateService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<DietaryProgramTemplate> dietaryProgramTemplateService;
    
    /**
     * 
     * @param fitnessGroupId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-fitness-group")
    public ModelAndView index(@PathVariable("fitnessGroupId") Long fitnessGroupId) {
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/special-courseware-template-index");
        
        FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(fitnessGroupId);
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        SpecialCoursewareTemplate specialCoursewareTemplate = groupTemplateService.loadSpecialCoursewareTemplate(fitnessGroup);
        modelAndView.addObject("specialCoursewareTemplate", specialCoursewareTemplate);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param dietType
     * @return
     */
    @RequestMapping(value = "/{specialId}/diet", method = RequestMethod.GET)
    public ModelAndView editDiet(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                 @PathVariable("specialId") Long specialId,
                                 @RequestParam("dietType") String dietType) {
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/special-courseware-template-diet");
        
        SpecialCoursewareTemplate specialCoursewareTemplate = groupTemplateService.loadSpecialCoursewareTemplate(specialId);
        modelAndView.addObject("dietaryProgramTemplate", specialCoursewareTemplate.getDiet(DietaryProgramType.aliasOf(dietType)));
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        modelAndView.addObject("dietType", dietType);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param dietType
     * @param dietaryProgramTemplateId
     * @return
     */
    @RequestMapping(value = "/{specialId}/diet", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDiet(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                           @PathVariable("specialId") Long specialId,
                                           @RequestParam("dietType") String dietType,
                                           @RequestParam("dietaryProgramTemplateId") Long dietaryProgramTemplateId){
        
        Result result = new Result();
            
        try{
            
            SpecialCoursewareTemplate specialCoursewareTemplate = groupTemplateService.loadSpecialCoursewareTemplate(specialId);
            DietaryProgramTemplate dietaryProgramTemplate = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, dietaryProgramTemplateId);
            
            groupTemplateService.editDiet(specialCoursewareTemplate, dietaryProgramTemplate, DietaryProgramType.aliasOf(dietType));
            
            result.success().message("修改饮食计划成功！");
            
        } catch (Exception e) {
            
            logger.error("onEditDiet - ", e);
            result.fail().message("修改饮食计划失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param batchId
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{specialId}/dietary-templates/{templateId}/snacks", method = RequestMethod.GET)
    public ModelAndView createSnack(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                    @PathVariable("specialId") Long specialId,
                                    @PathVariable("templateId") Long templateId) {
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/special-courseware-template-snack-create");
        
        DietaryProgramTemplate template = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, templateId);
        modelAndView.addObject("snacks", template.getSnacks());
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        modelAndView.addObject("templateId", templateId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @return
     */
    @RequestMapping(value = "/{specialId}/dietary-templates/{templateId}/snacks", method = RequestMethod.PATCH)
    public @ResponseBody String onCreateSnack(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                              @PathVariable("specialId") Long specialId,
                                              @PathVariable("templateId") Long templateId,
                                              @RequestParam("dietaryProgramTemplateId") Long dietaryProgramTemplateId){
        
        Result result = new Result();
            
        try{
            
            DietaryProgramTemplate template = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, templateId);
            
            groupTemplateService.createSnack(template, dietaryProgramTemplateId);
            
            result.success().message("修改饮食计划成功！");
            
        } catch (Exception e) {
            
            logger.error("onEditSnack - ", e);
            result.fail().message("修改饮食计划失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @return
     */
    @RequestMapping(value = "/{specialId}/rest", method = RequestMethod.GET)
    public ModelAndView rest(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                             @PathVariable("specialId") Long specialId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/special-courseware-template-rest");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @return
     */
    @RequestMapping(value = "/{specialId}/rest", method = RequestMethod.PATCH)
    public @ResponseBody String onRest(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                       @PathVariable("specialId") Long specialId){
        
        Result result = new Result();
        
        try{
            
            SpecialCoursewareTemplate specialCoursewareTemplate = groupTemplateService.loadSpecialCoursewareTemplate(specialId);
            
            groupTemplateService.rest(specialCoursewareTemplate);
            
            result.success().message("设为休息日成功！");
        }
        catch (Exception e) {
            
            logger.error("onRest - ", e);
            result.fail().message("设为休息日失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates/create", method = RequestMethod.GET)
    public ModelAndView createCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                                 @PathVariable("specialId") Long specialId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/courseware-template-create");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates", method = RequestMethod.POST)
    public @ResponseBody String onCreateCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                                           @PathVariable("specialId") Long specialId,
                                                           CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            SpecialCoursewareTemplate specialCoursewareTemplate = groupTemplateService.loadSpecialCoursewareTemplate(specialId);
            
            groupTemplateService.createCoursewareTemplate(specialCoursewareTemplate, coursewareTemplate);
            
            result.success().message("添加动作成功！");
        }
        catch (Exception e) {
            
            logger.error("onCreateCoursewareTemplate - ", e);
            result.fail().message("添加动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates/{coursewareTemplateId}/edit", method = RequestMethod.GET)
    public ModelAndView editCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                               @PathVariable("specialId") Long specialId,
                                               @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/courseware-template-edit");
        
        CoursewareTemplate coursewareTemplate = groupTemplateService.loadCoursewareTemplate(coursewareTemplateId);
        modelAndView.addObject("coursewareTemplate", coursewareTemplate);
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates/{coursewareTemplateId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                                         @PathVariable("specialId") Long specialId,
                                                         @Preloading("coursewareTemplateId") CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            groupTemplateService.editCoursewareTemplate(coursewareTemplate);
            
            result.success().message("修改动作成功！");
        }
        catch (Exception e) {
            
            logger.error("onEditCoursewareTemplate - ", e);
            result.fail().message("修改动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates/{coursewareTemplateId}/delete", method = RequestMethod.GET)
    public ModelAndView deleteCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                                 @PathVariable("specialId") Long specialId,
                                                 @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/special-courseware-template/courseware-template-delete");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("specialId", specialId);
        modelAndView.addObject("coursewareTemplateId", coursewareTemplateId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param specialId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{specialId}/courseware-templates/{coursewareTemplateId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDeleteCoursewareTemplate(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                                           @PathVariable("specialId") Long specialId,
                                                           @Preloading("id") CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            groupTemplateService.deleteCoursewareTemplate(coursewareTemplate);
            
            result.success().message("删除动作成功！");
        }
        catch (Exception e) {
            
            logger.error("onDeleteCoursewareTemplate - ", e);
            result.fail().message("删除动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }

}
